<template>
  <div class="order container">
    <header>
      <i class="iconfont icon-return" @click="$router.back()"></i>
      <span>提交订单</span>
      <i class="iconfont icon-home" @click="$router.replace('/home')"></i>
    </header>

    <section>
      <div class="path">
        <h3 class="path-title">收货信息</h3>
        <div class="path-content" @click="goPath">
          <div>
            <span>{{ path.name }}</span>
            <span>{{ path.tel }}</span>
          </div>
          <div>
            <span>{{ path.country }}</span>
            <span>{{ path.city }}</span>
            <span>{{ path.county }}</span>
            <span>{{ path.addressDetail }}</span>
          </div>
        </div>
      </div>

      <div class="payment">
        <div class="payMent-title">支付方式：</div>
        <van-radio-group v-model="radioPayment">
          <van-radio name="vx"> 微信支付</van-radio>
          <van-radio name="ali">支付宝支付</van-radio>
        </van-radio-group>
      </div>

      <div class="goods">
        <ul>
          <li v-for="(item, index) in goodList" :key="index">
            <div>
              <img :src="item.goods_imgUrl" alt="" />
            </div>
            <div class="goods-content">
              <h4>{{ item.goods_name }}</h4>
              <div class="goods-total">
                <span class="price">￥{{ item.goods_price }}.00</span>
                <span>x{{ item.goods_num }}</span>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </section>

    <footer>
      <div class="order-total">
        <span>共</span>
        <b>{{ total.num }}</b>
        <span>件</span>
        <span>总金额</span>
        <em>￥{{ total.price }}</em>
      </div>
      <div class="order-toPay" @click="goPayment">提交订单</div>
    </footer>
  </div>
</template>

<script>
import http from "@/common/api/request.js";
import { mapState, mapGetters, mapMutations } from "vuex";
import { Toast } from "mint-ui";

//引入增加安全性的序列化库qs
import qs from "qs";

//引入bus
import bus from "@/common/bus.js";

export default {
  data() {
    return {
      radioPayment: "vx",
      path: {},
      item: [],
      goodList: [],
      total: {
        price: 0,
        num: 0,
      },
    };
  },
  computed: {
    ...mapState({
      // list:state=>state.cart.list,
      order_id: (state) => state.order.order_id,
      selectList: (state) => state.cart.selectList,
    }),

    ...mapGetters(["defaultPath"]),
  },
  created() {
    this.selectAddress();
  },
  activated() {
    bus.$on(
      "selectPath",
      function (data) {
        this.path = JSON.parse(data);
      }.bind(this)
    );

    //选中商品的id号
    this.item = JSON.parse(this.$route.query.detail);
    this.goodList = JSON.parse(this.$route.query.goodList);

    this.selectOrder();
  },
  methods: {
    ...mapMutations(["initData"]),

    //查询数据
    selectAddress() {
      http
        .$axios({
          url: "/api/selectAddress",
          method: "post",
          headers: {
            token: true,
          },
        })
        .then((res) => {
          this.initData(res.data);
          //如果有默认地址
          if (this.defaultPath.length) {
            this.path = this.defaultPath[0];
          } else {
            this.path = res.data[0];
          }
        });
    },
    //查询订单
    selectOrder() {
      //查询订单
      http
        .$axios({
          url: "/api/selectOrder",
          method: "post",
          headers: {
            token: true,
          },
          data: {
            order_id: this.order_id,
          },
        })
        .then((res) => {
          this.total = {
            price: res.data[0].goods_price,
            num: res.data[0].goods_num,
          };
        });
    },

    //选择地址页
    goPath() {
      this.$router.push({
        path: "/path",
        query: {
          type: "select",
        },
      });
    },

    //提交订单
    goPayment() {
		console.log('this.path',this.path)
      //判断是否选择收获地址
      if (!this.path || this.path=='add' ) return Toast("请选择收货地址");
	  
	  if(this.radioPayment == 'ali'){
		    //发送请求，1.修改订单状态。 2.删除购物车内提交的商品
		    http
		      .$axios({
		        url: "/api/submitOrder",
		        method: "post",
		        headers: {
		          token: true,
		          // "content-type":'application/x-form-urlencoded'//支付时需要这句
		        },
		        data: {
		          orderId: this.order_id,
		          shopArr: this.selectList,
		        },
		      })
		      .then((res) => {
		        console.log(res);
		        let newArr = [];
		        this.goodList.forEach((v) => {
		          newArr.push(v.goods_name);
		        });
		    
		        //支付传递的参数
		        let dataOrder = {
		          orderId: this.order_id,
		          name: newArr.join(""),
		          price: this.total.price,
		        };
		    
		        if (res.success) {
		          //去支付
		          http
		            .$axios({
		              url: "/api/payment",
		              method: "post",
		              headers: {
		                token: true,
		                "Content-type": "application/x-www-form-urlencoded", //支付时需要这句
		              },
		              //qs增加安全性的序列化方法
		              data: qs.stringify(dataOrder),
		            })
		            .then((res) => {
		              if (res.success) {
		                //打开支付宝支付页面
		                window.location.href = `${res.paymentUrl}`;
		                // window.open(res.paymentUrl)
		              }
		              //返回一个rul
		              console.log(res);
		            });
		        }
		      });
	  }else{
		  return Toast("只能支付宝支付");
	  }

    },
  },
};
</script>

<style scoped lang="less">
.order {
  header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 1rem;
    color: #fff;
    background-color: #b0352f;

    i {
      padding: 0 0.4rem;
      font-size: 0.5rem;
    }
    span {
      padding: 0 0.4rem;
      font-size: 0.4rem;
    }
  }

  section {
    background-color: #f7f7f7;

    .path {
      .path-title {
        padding: 0.2rem 0.4rem;
        font-size: 0.45rem;
      }
      .path-content {
        padding: 0.14rem 0.4rem;
        font-size: 0.3rem;
        background-color: #fff;

        span {
          padding-right: 0.16rem;
        }
      }
    }

    .payment {
      padding: 0.16rem 0.4rem;
      margin-top: 0.4rem;
      font-size: 0.4rem;
      background-color: #fff;

      .van-radio-group {
        display: flex;
        padding: 0.16rem 0;

        .van-radio {
          padding-right: 0.5rem;
        }
      }
    }

    .goods {
      padding: 0.16rem 0.4rem;
      margin-top: 0.4rem;
      font-size: 0.4rem;
      background-color: #fff;

      ul {
        // width: 100%;

        li {
          display: flex;
          // width: 100%;
          margin-bottom: 0.2rem;
          border-bottom: 2px solid #f7f7f7;

          img {
            width: 2rem;
            height: 2rem;
          }
          .goods-content {
            width: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            padding-left: 0.4rem;

            .goods-total {
              // flex: 1;
              display: flex;
              justify-content: space-between;

              .price {
                color: #b0352f;
              }
            }
          }
        }
      }
    }
  }

  footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 1.2rem;
    border-top: 2px solid #f5f5f5;
    font-size: 0.4rem;
    .order-total {
      padding-left: 0.2rem;
      span {
        padding: 0 0.1rem;
      }
      b {
        color: #b0352f;
      }
      em {
        font-size: 0.5rem;
        color: #b0352f;
      }
    }
    .order-toPay {
      background-color: #b0352f;
      height: 1.2rem;
      width: 3rem;
      line-height: 1.2rem;
      text-align: center;
      color: #fff;
    }
  }
}
</style>
